﻿using NCommonLib.Thread;
using NCommonLib.Extensions;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Threading;

#if NETFX_CORE
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.ApplicationModel;
#else
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Threading.Tasks;
#endif

namespace NCache
{
    public class ImageCacheModel : INotifyPropertyChanged
    {
        #region Fields

        private string _cacheUrl;
        private ImageSource _image;
        private string _tokenTag = string.Empty;

        #endregion

        #region Properties

        public string CacheUrl
        {
            get { return _cacheUrl; }
            set
            {
                _cacheUrl = value;
                if (!string.IsNullOrEmpty(TokenTag))
                {
                    CacheImage();
                }
            }
        }

        public string TokenTag
        {
            get { return _tokenTag; }
            set
            {
                _tokenTag = value;
                if (!string.IsNullOrEmpty(CacheUrl))
                {
                    CacheImage();
                }
            }
        }


        public ImageSource Image
        {
            get { return _image; }
            set
            {
                if (value != _image)
                {
                    _image = value;
                    this.NotifyPropertyChanged("Image");
                }
            }
        }

       
        #endregion

        #region Method

        private async void CacheImage()
        {

#if NETFX_CORE
            if (DesignMode.DesignModeEnabled)
            {
                return;
            }
#else
            if (DesignerProperties.IsInDesignTool)
            {
                return;
            }
#endif
            if (!string.IsNullOrEmpty(_cacheUrl))
            {
                try
                {
                    BitmapImage bitmapImage = new BitmapImage();
                    CancellationToken cancelToken = CancellationTokenFactory.Instance.GetCancelToken(TokenTag).Token;
                    //队列Cache请求
                    Stream stream = await CacheEngine.QueueCacheImage(new Uri(_cacheUrl, UriKind.RelativeOrAbsolute)).WithCancellation(cancelToken);
                    //普通Cache请求
                    //Stream stream = await CacheEngine.CacheImage(new Uri(_cacheUrl, UriKind.RelativeOrAbsolute));
                    if (stream != null)
                    {
#if NETFX_CORE
                        bitmapImage.SetSource(await stream.ToRandomStream());
#else

                        bitmapImage.SetSource(stream);
#endif
                        stream.Dispose();
                        stream = null;
                    }
                    Image = bitmapImage;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("ImageCacheModel cache image ex:" + ex.Message.ToString());
                }
            }
        }

        #endregion

        #region NotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
